import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';

import { NzModalRef, NzModalService } from 'ng-zorro-antd/modal';

/**
 * 确认框
 */
@Injectable({ providedIn: 'root' })
export class ConfirmModalService {
  constructor(private modalSrc: NzModalService) {}

  info<T>(content: string): Subject<T> {
    return this.common('需要确认操作', content);
  }

  danger<T>(content: string): Subject<T> {
    return this.common('需要确认操作', content, true);
  }

  private common<T>(title: string, content: string, danger = false): Subject<T> {
    const modal: NzModalRef = this.modalSrc.confirm<boolean>({
      nzTitle: title,
      nzContent: content,
      nzOkDanger: danger,
      nzOnOk: () => modal.destroy(true),
      nzOnCancel: () => modal.destroy(false)
    });

    return modal.afterClose;
  }
}
